package com.lcq.gulimall.product.service.impl;

import com.lcq.gulimall.product.dao.AttrDao;
import com.lcq.gulimall.product.vo.AttrRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.Query;

import com.lcq.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.lcq.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lcq.gulimall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {
    @Autowired
    AttrDao attrDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrRespVo> queryAttrRelationList(Long gourpId) {
        List<AttrAttrgroupRelationEntity> list = this.getBaseMapper().selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",gourpId));
        List<Long> ids = list.stream().map((entity)->{
            return entity.getAttrId();
        }).collect(Collectors.toList());

        if(ids==null || ids.size()<=0){
            return null;
        }

        return attrDao.selectBatchIds(ids).stream().map((entity)->{
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(entity,attrRespVo);
            return attrRespVo;
        }).collect(Collectors.toList());
    }
}